package com.battery.web.controller.system.battery;

import java.util.List;

import com.battery.common.utils.DateUtils;
import com.battery.common.utils.ali.AliPayEntity;
import com.battery.framework.config.properties.AliPayResource;
import com.battery.system.domain.BO.RefundApplyBO;
import com.battery.system.domain.VO.RefundApplyVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.RefundApply;
import com.battery.system.service.IRefundApplyService;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 订单退款申请Controller
 *
 * @author battery
 * @date 2021-03-22
 */
@Controller
@RequestMapping("/system/refundApply")
public class RefundApplyController extends BaseController {


    private String prefix = "system/refundApply";

    @Autowired
    private IRefundApplyService refundApplyService;


    @Autowired
    private AliPayResource aliPayResource;

    private AliPayEntity _callApi() {
        AliPayEntity entity = new AliPayEntity();
        entity.setAppId(aliPayResource.getAppId());
        entity.setAlipayPublicKey(aliPayResource.getAlipayPublicKey());
        entity.setCharset(aliPayResource.getCharset());
        entity.setGatewayUrl(aliPayResource.getGatewayUrl());
        entity.setMerchantPrivateKey(aliPayResource.getMerchantPrivateKey());
        entity.setSignType(aliPayResource.getSignType());
        entity.setNotifyUrl(aliPayResource.getNotifyUrl());
        entity.setPayeeLogonId(aliPayResource.getPayeeLogonId());
        entity.setPayeeUserId(aliPayResource.getPayeeUserId());
        entity.setFreezeNotifyUrl(aliPayResource.getFreezeNotifyUrl());
        entity.setServiceId(aliPayResource.getServiceId());
        return entity;
    }


    @RequiresPermissions("system:refundApply:view")
    @GetMapping()
    public String refundApply() {
        return prefix + "/refundApply";
    }

    /**
     * 查询订单退款申请列表
     */
    @RequiresPermissions("system:refundApply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RefundApplyBO refundApplyBO) {
        startPage();
        List<RefundApplyVO> list = refundApplyService.selectRefundApplyVOList(refundApplyBO);
//        List<RefundApply> list = refundApplyService.selectRefundApplyList(refundApply);
        return getDataTable(list);
    }

    /**
     * 导出订单退款申请列表
     */
    @RequiresPermissions("system:refundApply:export")
    @Log(title = "订单退款申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RefundApply refundApply) {
        List<RefundApply> list = refundApplyService.selectRefundApplyList(refundApply);
        ExcelUtil<RefundApply> util = new ExcelUtil<RefundApply>(RefundApply.class);
        return util.exportExcel(list, "refundApply");
    }

    /**
     * 新增订单退款申请
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存订单退款申请
     */
    @RequiresPermissions("system:refundApply:add")
    @Log(title = "订单退款申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RefundApply refundApply) {
        return toAjax(refundApplyService.insertRefundApply(refundApply));
    }

    /**
     * 修改订单退款申请
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        RefundApply refundApply = refundApplyService.selectRefundApplyById(id);
        mmap.put("refundApply", refundApply);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单退款申请
     */
    @RequiresPermissions("system:refundApply:edit")
    @Log(title = "订单退款申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RefundApply refundApply) {
        return toAjax(refundApplyService.updateRefundApply(refundApply));
    }

    /**
     * 删除订单退款申请
     */
    @RequiresPermissions("system:refundApply:remove")
    @Log(title = "订单退款申请", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(refundApplyService.deleteRefundApplyByIds(ids));
    }


    /**
     * 跳转到拒绝申请操作页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/refuseCz/{id}")
    public String refuseCz(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/refuseCz";
    }


    @Log(title = "订单退款申请拒绝操作", businessType = BusinessType.OTHER)
    @RequiresPermissions("system:refundApply:refuseCz")
    @PostMapping("/refuseCz")
    @ResponseBody
    public AjaxResult refuseCz(RefundApply refundApply) {
        if (refundApply.getId() == null) {
            return error("主键ID为空");
        }

        if (StringUtils.isBlank(refundApply.getRemark())) {
            return error("主键ID为空");
        }

        // 审核状态 1、审核中，2、已通过，3、已拒绝
        refundApply.setAuditStatus(3);
        refundApply.setAuditTime(DateUtils.getNowDate());


        return toAjax(refundApplyService.updateByPrimaryKeySelective(refundApply));
    }


    /**
     * @param id
     * @return
     */
    @Log(title = "订单退款申请通过并退款", businessType = BusinessType.OTHER)
    @PostMapping("/adoptRefund")
    @ResponseBody
    public AjaxResult adoptRefund(String id) {
        if (StringUtils.isBlank(id)) {
            return error("主键ID不能为空");
        }

        AliPayEntity aliPayEntity = _callApi();
        return refundApplyService.adoptRefund(id, aliPayEntity);
    }


}
